﻿Imports BUS
Imports DTO


Public Class frmSLSachTra
    Dim bt As Button
    Dim temp As DataTable

    Sub New(ByRef bt As Button)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.bt = bt
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Me.Close()
    End Sub

    Private Sub frmSLSachTra_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        temp = ChiTietPhieuMuon_BUS.loadCTPhieuMuon_PhieuMuon(bt.Name, bt.Tag)
    End Sub

    Private Sub nbSL_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nbSL.ValueChanged
        If nbSL.Value > temp(0)(2) Then
            nbSL.Value = nbSL.Value - 1
            MessageBox.Show("Đã vượt quá số lượng mượn")
            Return
        End If
    End Sub

    Private Sub btnTra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTra.Click
        If nbSL.Value = 0 Then
            MessageBox.Show("Vui lòng chọn số lượng")
            Return
        End If

        Dim pt As New PhieuTra_DTO
        pt.maMuon = temp(0)(0)
        pt.maDauSach = temp(0)(1)
        pt.ngayTra = DateTime.Now
        pt.slSach = nbSL.Value
        pt.tienMuon = temp(0)(3)
        Dim d As DateTime = temp(0)(4)
        If DateTime.Now.DayOfYear - d.DayOfYear >= 1 Then
            pt.tienPhat = pt.tienMuon
        ElseIf DateTime.Now.DayOfYear - d.DayOfYear >= 7 Then
            pt.tienPhat = pt.tienMuon * 2
        ElseIf DateTime.Now.DayOfYear - d.DayOfYear >= 15 Then
            pt.tienPhat = pt.tienMuon * 5
        End If

        If frmDangNhap.dsSachTra.Count >= 1 Then
            For i As Integer = 0 To frmDangNhap.dsSachTra.Count - 1
                If frmDangNhap.dsSachTra(i).maDauSach = pt.maDauSach Then
                    If frmDangNhap.dsSachTra(i).slSach + pt.slSach >= temp(0)(2) Then
                        MessageBox.Show("Đã vượt quá số lượng mượn!")
                        Return
                    End If
                    frmDangNhap.dsSachTra(i).slSach = _
                        frmDangNhap.dsSachTra(i).slSach + pt.slSach
                    MessageBox.Show("Đã đưa vào danh sách trả")
                    Return
                End If
            Next
        End If

        frmDangNhap.dsSachTra.Add(pt)
        MessageBox.Show("Đã đưa vào danh sách trả")
    End Sub
End Class